<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-rpc-state-settings-panel" [formGroup]="updateStateSettingsFormGroup">
  <div class="tb-rpc-state-settings-title">{{ panelTitle | translate }}</div>
  <div class="tb-rpc-state-settings-panel-content">
    <div class="tb-form-row">
      <div class="fixed-title-width" >{{ 'widgets.rpc-state.action' | translate }}</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <mat-select formControlName="action">
          <mat-option *ngFor="let action of rpcUpdateStateActions" [value]="action">
            {{ rpcUpdateStateTranslationsMap.get(action) | translate }}
          </mat-option>
        </mat-select>
      </mat-form-field>
    </div>
    <ng-container [ngSwitch]="updateStateSettingsFormGroup.get('action').value">
      <ng-template [ngSwitchCase]="rpcUpdateStateAction.EXECUTE_RPC">
        <div class="tb-form-row" formGroupName="executeRpc">
          <div class="fixed-title-width" >{{ 'widgets.rpc-state.method' | translate }}*</div>
          <mat-form-field class="flex tb-suffix-absolute" appearance="outline" subscriptSizing="dynamic">
            <input matInput required formControlName="method" placeholder="{{ 'widget-config.set' | translate }}">
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="'widgets.rpc-state.method-name-required' | translate"
                      *ngIf="updateStateSettingsFormGroup.get('executeRpc').get('method').hasError('required')
                              && updateStateSettingsFormGroup.get('executeRpc').get('method').touched"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
        </div>
      </ng-template>
      <ng-template [ngSwitchCase]="rpcUpdateStateAction.SET_ATTRIBUTE">
        <ng-container formGroupName="setAttribute">
          <div class="tb-form-row">
            <div class="fixed-title-width">{{ 'widgets.rpc-state.attribute-scope' | translate }}</div>
            <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
              <mat-select formControlName="scope">
                <mat-option *ngFor="let scope of attributeScopes" [value]="scope">
                  {{ telemetryTypeTranslationsMap.get(scope) | translate }}
                </mat-option>
              </mat-select>
            </mat-form-field>
          </div>
          <div class="tb-form-row">
            <div class="fixed-title-width" >{{ 'widgets.rpc-state.attribute-key' | translate }}*</div>
            <tb-device-key-autocomplete
              fxFlex
              formControlName="key"
              required
              requiredText="widgets.rpc-state.attribute-key-required"
              inlineField
              [aliasController]="aliasController"
              [targetDevice]="targetDevice"
              [keyType]="dataKeyType.attribute"
              [attributeScope]="updateStateSettingsFormGroup.get('setAttribute').get('scope').value">
            </tb-device-key-autocomplete>
          </div>
        </ng-container>
      </ng-template>
      <ng-template [ngSwitchCase]="rpcUpdateStateAction.ADD_TIME_SERIES">
        <ng-container formGroupName="putTimeSeries">
          <div class="tb-form-row">
            <div class="fixed-title-width" >{{ 'widgets.rpc-state.time-series-key' | translate }}*</div>
            <tb-device-key-autocomplete
              fxFlex
              formControlName="key"
              required
              requiredText="widgets.rpc-state.time-series-key-required"
              inlineField
              [aliasController]="aliasController"
              [targetDevice]="targetDevice"
              [keyType]="dataKeyType.timeseries">
            </tb-device-key-autocomplete>
          </div>
        </ng-container>
      </ng-template>
    </ng-container>
    <div class="tb-form-panel stroked" formGroupName="stateToParams">
      <div class="tb-form-row no-padding no-border column-xs">
        <div class="fixed-title-width">{{ (updateStateSettingsFormGroup.get('action').value === rpcUpdateStateAction.EXECUTE_RPC ?
          'widgets.rpc-state.parameters' : 'widgets.rpc-state.value') | translate }}</div>
        <tb-toggle-select fxFlex formControlName="type">
          <tb-toggle-option [value]="stateToParamsType.CONSTANT">{{ 'widgets.rpc-state.converter-constant' | translate }}</tb-toggle-option>
          <tb-toggle-option [value]="stateToParamsType.FUNCTION">{{ 'widgets.rpc-state.converter-function' | translate }}</tb-toggle-option>
          <tb-toggle-option *ngIf="updateStateSettingsFormGroup.get('action').value === rpcUpdateStateAction.EXECUTE_RPC"
                              [value]="stateToParamsType.NONE">{{ 'widgets.rpc-state.converter-none' | translate }}</tb-toggle-option>
        </tb-toggle-select>
      </div>
      <tb-value-input *ngIf="updateStateSettingsFormGroup.get('stateToParams').get('type').value === stateToParamsType.CONSTANT"
                      fxFlex
                      [layout]="{
                              layout: 'row',
                              breakpoints: {xs: 'column'}
                          }"
                      formControlName="constantValue">
      </tb-value-input>
      <tb-js-func *ngIf="updateStateSettingsFormGroup.get('stateToParams').get('type').value === stateToParamsType.FUNCTION"
                  formControlName="stateToParamsFunction"
                  required
                  [globalVariables]="functionScopeVariables"
                  [functionArgs]="['value']"
                  functionTitle="{{ 'widgets.rpc-state.convert-value-function' | translate }}"
                  helpId="widget/lib/rpc/convert_value_fn">
      </tb-js-func>
    </div>
    <div *ngIf="updateStateSettingsFormGroup.get('action').value === rpcUpdateStateAction.EXECUTE_RPC"
         class="tb-form-panel no-border tb-slide-toggle" formGroupName="executeRpc">
      <mat-expansion-panel class="tb-settings no-padding">
        <mat-expansion-panel-header fxLayout="row wrap">
          <mat-panel-title>
          </mat-panel-title>
          <mat-panel-description fxLayoutAlign="end center" translate>
            widget-config.advanced-settings
          </mat-panel-description>
        </mat-expansion-panel-header>
        <ng-template matExpansionPanelContent>
          <div class="tb-form-row space-between">
            <div>{{ 'widgets.rpc-state.request-timeout-ms' | translate }}</div>
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput formControlName="requestTimeout" type="number" required min="5000" step="1" placeholder="{{ 'widget-config.set' | translate }}">
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="(updateStateSettingsFormGroup.get('executeRpc').get('requestTimeout').hasError('required')
                                        ? 'widgets.rpc-state.request-timeout-required' : 'widgets.rpc-state.min-request-timeout-error') | translate"
                        *ngIf="updateStateSettingsFormGroup.get('executeRpc').get('requestTimeout').touched &&
                               updateStateSettingsFormGroup.get('executeRpc').get('requestTimeout').invalid"
                        class="tb-error">
                warning
              </mat-icon>
            </mat-form-field>
          </div>
          <div class="tb-form-panel stroked tb-slide-toggle">
            <mat-expansion-panel class="tb-settings"
                                 [expanded]="updateStateSettingsFormGroup.get('executeRpc').get('requestPersistent').value"
                                 [disabled]="!updateStateSettingsFormGroup.get('executeRpc').get('requestPersistent').value">
              <mat-expansion-panel-header fxLayout="row wrap">
                <mat-panel-title>
                  <mat-slide-toggle class="mat-slide" formControlName="requestPersistent" (click)="$event.stopPropagation()"
                                    fxLayoutAlign="center">
                    {{ 'widgets.rpc-state.request-persistent' | translate }}
                  </mat-slide-toggle>
                </mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent>
                <div class="tb-form-row space-between">
                  <div tb-hint-tooltip-icon="{{'widgets.rpc-state.persistent-polling-interval-hint' | translate}}">{{ 'widgets.rpc-state.persistent-polling-interval' | translate }}</div>
                  <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
                    <input matInput formControlName="persistentPollingInterval" type="number" required min="1000" placeholder="{{ 'widget-config.set' | translate }}">
                    <mat-icon matSuffix
                              matTooltipPosition="above"
                              matTooltipClass="tb-error-tooltip"
                              [matTooltip]="(updateStateSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').hasError('required')
                                          ? 'widgets.rpc-state.persistent-polling-interval-required' : 'widgets.rpc-state.min-persistent-polling-interval-error') | translate"
                              *ngIf="updateStateSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').touched &&
                                 updateStateSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').invalid"
                              class="tb-error">
                      warning
                    </mat-icon>
                  </mat-form-field>
                </div>
              </ng-template>
            </mat-expansion-panel>
          </div>
        </ng-template>
      </mat-expansion-panel>
    </div>
  </div>
  <div class="tb-rpc-state-settings-panel-buttons">
    <button mat-button
            color="primary"
            type="button"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button
            color="primary"
            type="button"
            (click)="applyUpdateStateSettings()"
            [disabled]="updateStateSettingsFormGroup.invalid || !updateStateSettingsFormGroup.dirty">
      {{ 'action.apply' | translate }}
    </button>
  </div>
</div>
